﻿using BITUI.Audio;
using SharpAudio.Codec;
using SharpAudio.Codec.Wave;
using System;
using System.Collections.Generic;
using System.Text;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace ttbit.Audio
{
    //AudioClip意味着要经常重复使用这个音源
    public class AudioClip : TTEngine.ISoundClip
    {
        public string Name;
        internal float[] alldata;
        internal float time;

        public void Dispose()
        {
            alldata = null;
        }
        [ThreadStatic]
        static float[] _buf = new float[4096];
        public static AudioClip Create(IAudioData data)
        {
            if (_buf == null)
                _buf = new float[4096];
            AudioClip clip = new AudioClip();
            clip.time = data.Time;

            List<float> allbuf = new List<float>();
            while (true)
            {
                int readlen = AudioDataConverter.ReadFloat_44100_2(data, _buf, 0, _buf.Length);
                if (readlen == 0)
                    break;
                for (var i = 0; i < readlen; i++)
                    allbuf.Add(_buf[i]);
            }
            clip.alldata = allbuf.ToArray();
            return clip;
        }
        public static AudioClip CreateFromData(System.IO.Stream stream)
        {
            var head = stream.ReadFourCc();
            var ismp3 = false;

            if (head[0] == 0xff && head[1] == 0xfb)//mp3
                ismp3 = true;
            else if (head[0] == 'I' && head[1] == 'D' && head[2] == '3')
                ismp3 = true;

            if (ismp3)
            {
                var data = new AudioData_Mp3();
                data.Load(stream);
                var a = AudioClip.Create(data);
                data.Dispose();
                return a;
            }
            else if (head == "OggS")
            {
                var data = new AudioData_Ogg();
                data.Load(stream);
                var a = AudioClip.Create(data);
                data.Dispose();
                return a;
            }
            else if (head == "RIFF")
            {
                var data = new SharpAudio.Codec.Wave.AudioData_Wav();
                data.Load(stream);
                var a = AudioClip.Create(data);
                data.Dispose();
                return a;
            }
            throw new Exception("unknown format");

        }
        public static AudioClip CreateFromFile(string filename)
        {
            var ext = System.IO.Path.GetExtension(filename).ToLower();
            if (ext == ".ogg")
            {
                var data = new AudioData_Ogg();
                data.Load(filename);
                var a = AudioClip.Create(data);
                data.Dispose();
                return a;
            }
            else if (ext == ".mp3")
            {
                var data = new AudioData_Mp3();
                data.Load(filename);
                var a = AudioClip.Create(data);
                data.Dispose();
                return a;
            }
            else
            {
                throw new Exception("error data.");
            }
        }
    }
}
